package user_assets_pix

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserPixWalletInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户Pix钱包信息 - 查询
func NewUserPixWalletInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPixWalletInfoLogic {
	return &UserPixWalletInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserPixWalletInfoLogic) UserPixWalletInfo() (resp *types.UserPixWalletsInfoResponse, err error) {
	request := rpc.UserPixWalletsInfoRequest{}
	copier.Copy(&request, &types.UserPixWalletsInfoRequest{})
	request.UserId = headInfo.GetTokenUid(l.ctx)
	if request.UserId == 0 {
		return &types.UserPixWalletsInfoResponse{
			UserPixWalletsInfoList: []*types.UserPixWalletsInfo{},
		}, errors.New("user is not exist")
	}
	info, err := l.svcCtx.AssetsRpc.UserChannel.UserPixWalletsInfo(l.ctx, &request)
	var dataList []*types.UserPixWalletsInfo

	if err != nil {
		return &types.UserPixWalletsInfoResponse{
			UserPixWalletsInfoList: dataList,
		}, err
	}
	if len(info.UserPixWalletsInfoList) == 0 {
		return &types.UserPixWalletsInfoResponse{
			UserPixWalletsInfoList: dataList,
		}, nil
	}
	_ = copier.Copy(&dataList, info.UserPixWalletsInfoList)

	return &types.UserPixWalletsInfoResponse{
		UserPixWalletsInfoList: dataList,
	}, nil
}
